package com.example.volunteer.service.impl;

import com.example.volunteer.mapper.PointsMapper;
import com.example.volunteer.model.Points;
import com.example.volunteer.service.PointsService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PointsServiceImpl implements PointsService {

    @Autowired
    private PointsMapper pointsMapper;

    @Override
    public PageResult<Points> findPointsWithPage(int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findPointsWithPage(calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countPoints(),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Points> findByVolunteerIdWithPage(String volunteerId, int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findByVolunteerIdWithPage(volunteerId, calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countByVolunteerId(volunteerId),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Points> findByActivityIdWithPage(Integer activityId, int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findByActivityIdWithPage(activityId, calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countByActivityId(activityId),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Points> findTop10VolunteersWithPage(int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findTop10VolunteersWithPage(calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countTop10Volunteers(),
                pageNum,
                pageSize
        );
    }

    @Override
    public PageResult<Points> findTop10VolunteersByPeriodWithPage(int months, int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findTop10VolunteersByPeriodWithPage(months, calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countTop10VolunteersByPeriod(months),
                pageNum,
                pageSize
        );
    }

    // 新增的分页查询方法
    public PageResult<Points> findByPointsIdWithPage(Integer pointsId, int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findByPointsIdWithPage(pointsId, calculateOffset(pageNum, pageSize) , pageSize),
                pointsMapper.countByPeriodId(pointsId),
                pageNum,
                pageSize
        );
    }

    public PageResult<Points> findByPointsEarnWithPage(int pointsEarned, int pageNum, int pageSize) {
        return createPageResult(
                pointsMapper.findByPointsEarnWithPage(pointsEarned, calculateOffset(pageNum, pageSize), pageSize),
                pointsMapper.countByPointsEarn(pointsEarned),
                pageNum,
                pageSize
        );
    }

    // 辅助方法
    private int calculateOffset(int pageNum, int pageSize) {
        return (pageNum - 1) * pageSize;
    }

    private PageResult<Points> createPageResult(List<Points> data, int total, int pageNum, int pageSize) {
        PageResult<Points> result = new PageResult<>();
        result.setData(data);
        result.setTotal(total);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setPages((int) Math.ceil((double) total / pageSize));
        return result;
    }
}